package Screen;



import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import Base.Constantes;
import Button.buttonBase;




public class Screen {
	//posicoes e tamanho de tela;
	private int x;
	private int y;
	private int width;
	private int height;
	private boolean Visible;
	//algumas telas pode ser mexidas;
	private boolean PossivelMovimentar;
	//sempre vai saber qual tela chamou a tela atual;
	private Screen telaAnterior;
	//fundo ou backgraud
	private BufferedImage backgruond;	
	// lista de botoes na tela 
	private ArrayList<buttonBase>listaDeBotoes = new ArrayList<buttonBase>();
	
	
	
	

	public Screen( int X, int Y, int Widht, int Height, boolean movel, Screen TelaAnterior) {
		// TODO Auto-generated constructor stub
		this.setX(X);
		this.setY(Y);
		this.setWidth(Widht);
		this.setHeight(Height);
		this.setPossivelMovimentar(movel);
		this.setTelaAnterior(TelaAnterior);
		this.setVisible(true);
		
	}
	public Screen( int X, int Y, int Widht, int Height, boolean movel, Screen TelaAnterior, String enderecoImage) {
		// TODO Auto-generated constructor stub
		this.setX(X);
		this.setY(Y);
		this.setWidth(Widht);
		this.setHeight(Height);
		this.setPossivelMovimentar(movel);
		this.setTelaAnterior(TelaAnterior);
		this.setBackgruond(Constantes.LoadImage(enderecoImage));		
		this.setVisible(true);
		/*
		 * adicionar botoes na lista atraves de um csv de acordo com o nome da tela
		 */
		//loadButton("/BotoesScreem.csv");
		
	}
	
	public void Draw(Graphics2D dbg){
		if(getBackgruond()!=null){
			dbg.drawImage(getBackgruond(),null, getX(), getY());
			//aqui vai ser desenhado todos os botoes 
			if(listaDeBotoes.size()>0){
				//System.out.println("tamanho da lista de botoes" +listaDeBotoes.size());
				for (int i=0; i<listaDeBotoes.size(); i++) {
					buttonBase botao = listaDeBotoes.get(i);
					botao.Draw(dbg);
				}
			}
		}else{
			dbg.setColor(Color.BLACK);
			dbg.fillRect(getX(), getY(), getXFinal(), getYFinal());
			
		}
	}
	/*
	 * A Funcao UPdate vai ficar responsavel por simular tudo que tiver ativo na tela
	 * 
	 */
	
	public void update(long diffTime){
		
		for(int i = 0; i<listaDeBotoes.size(); i++){
			if(listaDeBotoes.get(i).isMouseClicked()){
				listaDeBotoes.get(i).getTelaPraSerCarregada();
				System.out.println(listaDeBotoes.get(i).getTelaPraSerCarregada());
			}
		}
	}
	
	public void mouseOverButton(int mouseX, int mouseY){
		for(int i = 0; i<listaDeBotoes.size(); i++){
			listaDeBotoes.get(i).mouseOver(mouseX, mouseY);
		}
	}
	
	/*
	 * Para adicionar botoes na tela so adicionar no arquivo csv
	 */
	public void loadButton (String fileName){
		BufferedReader dados = null;
		String str = "";
		dados = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(fileName)));
	
		try {
			while ((str = dados.readLine()) != null){
				if(str.charAt(0)!='#'){
					String parametros[] = str.split(";");
					int x = Integer.parseInt(parametros[0]);
					//System.out.println("x "+x);
					int y = Integer.parseInt(parametros[1]);
					//System.out.println("y "+y);
					int Width =Integer.parseInt(parametros[2]);
					//System.out.println("wight "+Width);
					int Height = Integer.parseInt(parametros[3]);
					//System.out.println("Height "+Height);
					//System.out.println(" end screem a ser chamada  "+parametros[4]);
					//System.out.println(" end image a ser chamada  "+parametros[5]);
					//System.out.println(" end image a ser chamada  "+parametros[6]);
					
					listaDeBotoes.add(new buttonBase(x, y, Width, Height, parametros[4], parametros[5], parametros[6]));
				}
			}
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		}
		
	}
	

	
	public int getXFinal(){
		return getX()+getWidth();
	}
	public int getYFinal(){
		return getY()+getHeight();
	}


	public void setX(int x) {
		this.x = x;
	}


	public int getX() {
		return x;
	}


	public void setY(int y) {
		this.y = y;
	}


	public int getY() {
		return y;
	}


	public void setWidth(int width) {
		this.width = width;
	}


	public int getWidth() {
		return width;
	}


	public void setHeight(int height) {
		this.height = height;
	}


	public int getHeight() {
		return height;
	}


	public void setPossivelMovimentar(boolean possivelMovimentar) {
		PossivelMovimentar = possivelMovimentar;
	}


	public boolean isPossivelMovimentar() {
		return PossivelMovimentar;
	}


	public void setTelaAnterior(Screen telaAnterior) {
		this.telaAnterior = telaAnterior;
	}


	public Screen getTelaAnterior() {
		return telaAnterior;
	}
	

	public void setBackgruond(BufferedImage backgruond) {
		this.backgruond = backgruond;
	}

	public BufferedImage getBackgruond() {
		return backgruond;
	}
	public void setVisible(boolean visible) {
		Visible = visible;
	}
	public boolean isVisible() {
		return Visible;
	}
	
	public void clicked(){
		System.out.println("click");
		for(int i = 0; i<listaDeBotoes.size(); i++){
			listaDeBotoes.get(i).testClick();
		}
	}
	
	

}
